package com.ysu.demo.controller;


import com.ysu.demo.Local.MyLocal;
import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.annotation.UserToken;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.expend.ExpendPassForm;
import com.ysu.demo.from.expend.ExpendUploadForm;
import com.ysu.demo.from.expend.MoneyApplyForm;
import com.ysu.demo.service.IExpendService;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/expend")
public class ExpendController {
    @Autowired
    IExpendService expendService;

    @PostMapping("/money/apply") //学院申请
    @UserToken
    public ResultVO moneyApply(@RequestBody @Valid MoneyApplyForm form,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        if(!form.getCollegeId().equals(MyLocal.getId())){
            throw new BaseException(ExceptionEnum.NOT_PASS);
        }

        expendService.moneyApply(form);
        return ResultUtil.TrueResult();
    }

    @PostMapping("/pass")//审核通过
    public ResultVO passExpend(@RequestBody @Valid ExpendPassForm form,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }
        expendService.passExpend(form);
        return ResultUtil.TrueResult();
    }

    @PostMapping("/upload/expend")//上传支出凭证
    public ResultVO uploadExpend(@RequestBody @Valid ExpendUploadForm form,
                                 BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }
        expendService.uploadExpend(form);
        return ResultUtil.TrueResult();
    }

}
